import java.util.*;

public class Solution2363 {
    public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {
        Map<Integer,Integer> map=new HashMap<>();
        add(items1,map);
        add(items2,map);
        List<List<Integer>> lists=new ArrayList<>();
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            lists.add(Arrays.asList(entry.getKey(),entry.getValue()));
        }
        lists.sort(Comparator.comparingInt(a -> a.get(0)));
        return lists;
    }

    private void add(int[][] items, Map<Integer, Integer> map) {
        for (int[] item : items) {
            map.put(item[0],map.getOrDefault(item[0],0)+item[1]);
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution2363().mergeSimilarItems(new int[][]{{1,1},{4,5},{3,8}},new int[][]{{3,1},{1,5}}));
    }
}
